/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.admin.modeljpa;

import com.admin.modeljpa.exceptions.IllegalOrphanException;
import com.admin.modeljpa.exceptions.NonexistentEntityException;
import com.admin.modeljpa.exceptions.RollbackFailureException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.entities.EventRegistration;
import java.util.ArrayList;
import java.util.Collection;
import com.entities.EventComment;
import com.entities.EventGift;
import com.entities.EventLibrary;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;

/**
 *
 * @author Tuan Long
 */
public class EventLibraryJpaController implements Serializable {

    public EventLibraryJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(EventLibrary eventLibrary) throws RollbackFailureException, Exception {
        if (eventLibrary.getEventRegistrationCollection() == null) {
            eventLibrary.setEventRegistrationCollection(new ArrayList<EventRegistration>());
        }
        if (eventLibrary.getEventCommentCollection() == null) {
            eventLibrary.setEventCommentCollection(new ArrayList<EventComment>());
        }
        if (eventLibrary.getEventGiftCollection() == null) {
            eventLibrary.setEventGiftCollection(new ArrayList<EventGift>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Collection<EventRegistration> attachedEventRegistrationCollection = new ArrayList<EventRegistration>();
            for (EventRegistration eventRegistrationCollectionEventRegistrationToAttach : eventLibrary.getEventRegistrationCollection()) {
                eventRegistrationCollectionEventRegistrationToAttach = em.getReference(eventRegistrationCollectionEventRegistrationToAttach.getClass(), eventRegistrationCollectionEventRegistrationToAttach.getId());
                attachedEventRegistrationCollection.add(eventRegistrationCollectionEventRegistrationToAttach);
            }
            eventLibrary.setEventRegistrationCollection(attachedEventRegistrationCollection);
            Collection<EventComment> attachedEventCommentCollection = new ArrayList<EventComment>();
            for (EventComment eventCommentCollectionEventCommentToAttach : eventLibrary.getEventCommentCollection()) {
                eventCommentCollectionEventCommentToAttach = em.getReference(eventCommentCollectionEventCommentToAttach.getClass(), eventCommentCollectionEventCommentToAttach.getId());
                attachedEventCommentCollection.add(eventCommentCollectionEventCommentToAttach);
            }
            eventLibrary.setEventCommentCollection(attachedEventCommentCollection);
            Collection<EventGift> attachedEventGiftCollection = new ArrayList<EventGift>();
            for (EventGift eventGiftCollectionEventGiftToAttach : eventLibrary.getEventGiftCollection()) {
                eventGiftCollectionEventGiftToAttach = em.getReference(eventGiftCollectionEventGiftToAttach.getClass(), eventGiftCollectionEventGiftToAttach.getId());
                attachedEventGiftCollection.add(eventGiftCollectionEventGiftToAttach);
            }
            eventLibrary.setEventGiftCollection(attachedEventGiftCollection);
            em.persist(eventLibrary);
            for (EventRegistration eventRegistrationCollectionEventRegistration : eventLibrary.getEventRegistrationCollection()) {
                EventLibrary oldEventIdOfEventRegistrationCollectionEventRegistration = eventRegistrationCollectionEventRegistration.getEventId();
                eventRegistrationCollectionEventRegistration.setEventId(eventLibrary);
                eventRegistrationCollectionEventRegistration = em.merge(eventRegistrationCollectionEventRegistration);
                if (oldEventIdOfEventRegistrationCollectionEventRegistration != null) {
                    oldEventIdOfEventRegistrationCollectionEventRegistration.getEventRegistrationCollection().remove(eventRegistrationCollectionEventRegistration);
                    oldEventIdOfEventRegistrationCollectionEventRegistration = em.merge(oldEventIdOfEventRegistrationCollectionEventRegistration);
                }
            }
            for (EventComment eventCommentCollectionEventComment : eventLibrary.getEventCommentCollection()) {
                EventLibrary oldEventIdOfEventCommentCollectionEventComment = eventCommentCollectionEventComment.getEventId();
                eventCommentCollectionEventComment.setEventId(eventLibrary);
                eventCommentCollectionEventComment = em.merge(eventCommentCollectionEventComment);
                if (oldEventIdOfEventCommentCollectionEventComment != null) {
                    oldEventIdOfEventCommentCollectionEventComment.getEventCommentCollection().remove(eventCommentCollectionEventComment);
                    oldEventIdOfEventCommentCollectionEventComment = em.merge(oldEventIdOfEventCommentCollectionEventComment);
                }
            }
            for (EventGift eventGiftCollectionEventGift : eventLibrary.getEventGiftCollection()) {
                EventLibrary oldEventIdOfEventGiftCollectionEventGift = eventGiftCollectionEventGift.getEventId();
                eventGiftCollectionEventGift.setEventId(eventLibrary);
                eventGiftCollectionEventGift = em.merge(eventGiftCollectionEventGift);
                if (oldEventIdOfEventGiftCollectionEventGift != null) {
                    oldEventIdOfEventGiftCollectionEventGift.getEventGiftCollection().remove(eventGiftCollectionEventGift);
                    oldEventIdOfEventGiftCollectionEventGift = em.merge(oldEventIdOfEventGiftCollectionEventGift);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(EventLibrary eventLibrary) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            EventLibrary persistentEventLibrary = em.find(EventLibrary.class, eventLibrary.getEventId());
            Collection<EventRegistration> eventRegistrationCollectionOld = persistentEventLibrary.getEventRegistrationCollection();
            Collection<EventRegistration> eventRegistrationCollectionNew = eventLibrary.getEventRegistrationCollection();
            Collection<EventComment> eventCommentCollectionOld = persistentEventLibrary.getEventCommentCollection();
            Collection<EventComment> eventCommentCollectionNew = eventLibrary.getEventCommentCollection();
            Collection<EventGift> eventGiftCollectionOld = persistentEventLibrary.getEventGiftCollection();
            Collection<EventGift> eventGiftCollectionNew = eventLibrary.getEventGiftCollection();
            List<String> illegalOrphanMessages = null;
            for (EventRegistration eventRegistrationCollectionOldEventRegistration : eventRegistrationCollectionOld) {
                if (!eventRegistrationCollectionNew.contains(eventRegistrationCollectionOldEventRegistration)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain EventRegistration " + eventRegistrationCollectionOldEventRegistration + " since its eventId field is not nullable.");
                }
            }
            for (EventComment eventCommentCollectionOldEventComment : eventCommentCollectionOld) {
                if (!eventCommentCollectionNew.contains(eventCommentCollectionOldEventComment)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain EventComment " + eventCommentCollectionOldEventComment + " since its eventId field is not nullable.");
                }
            }
            for (EventGift eventGiftCollectionOldEventGift : eventGiftCollectionOld) {
                if (!eventGiftCollectionNew.contains(eventGiftCollectionOldEventGift)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain EventGift " + eventGiftCollectionOldEventGift + " since its eventId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<EventRegistration> attachedEventRegistrationCollectionNew = new ArrayList<EventRegistration>();
            for (EventRegistration eventRegistrationCollectionNewEventRegistrationToAttach : eventRegistrationCollectionNew) {
                eventRegistrationCollectionNewEventRegistrationToAttach = em.getReference(eventRegistrationCollectionNewEventRegistrationToAttach.getClass(), eventRegistrationCollectionNewEventRegistrationToAttach.getId());
                attachedEventRegistrationCollectionNew.add(eventRegistrationCollectionNewEventRegistrationToAttach);
            }
            eventRegistrationCollectionNew = attachedEventRegistrationCollectionNew;
            eventLibrary.setEventRegistrationCollection(eventRegistrationCollectionNew);
            Collection<EventComment> attachedEventCommentCollectionNew = new ArrayList<EventComment>();
            for (EventComment eventCommentCollectionNewEventCommentToAttach : eventCommentCollectionNew) {
                eventCommentCollectionNewEventCommentToAttach = em.getReference(eventCommentCollectionNewEventCommentToAttach.getClass(), eventCommentCollectionNewEventCommentToAttach.getId());
                attachedEventCommentCollectionNew.add(eventCommentCollectionNewEventCommentToAttach);
            }
            eventCommentCollectionNew = attachedEventCommentCollectionNew;
            eventLibrary.setEventCommentCollection(eventCommentCollectionNew);
            Collection<EventGift> attachedEventGiftCollectionNew = new ArrayList<EventGift>();
            for (EventGift eventGiftCollectionNewEventGiftToAttach : eventGiftCollectionNew) {
                eventGiftCollectionNewEventGiftToAttach = em.getReference(eventGiftCollectionNewEventGiftToAttach.getClass(), eventGiftCollectionNewEventGiftToAttach.getId());
                attachedEventGiftCollectionNew.add(eventGiftCollectionNewEventGiftToAttach);
            }
            eventGiftCollectionNew = attachedEventGiftCollectionNew;
            eventLibrary.setEventGiftCollection(eventGiftCollectionNew);
            eventLibrary = em.merge(eventLibrary);
            for (EventRegistration eventRegistrationCollectionNewEventRegistration : eventRegistrationCollectionNew) {
                if (!eventRegistrationCollectionOld.contains(eventRegistrationCollectionNewEventRegistration)) {
                    EventLibrary oldEventIdOfEventRegistrationCollectionNewEventRegistration = eventRegistrationCollectionNewEventRegistration.getEventId();
                    eventRegistrationCollectionNewEventRegistration.setEventId(eventLibrary);
                    eventRegistrationCollectionNewEventRegistration = em.merge(eventRegistrationCollectionNewEventRegistration);
                    if (oldEventIdOfEventRegistrationCollectionNewEventRegistration != null && !oldEventIdOfEventRegistrationCollectionNewEventRegistration.equals(eventLibrary)) {
                        oldEventIdOfEventRegistrationCollectionNewEventRegistration.getEventRegistrationCollection().remove(eventRegistrationCollectionNewEventRegistration);
                        oldEventIdOfEventRegistrationCollectionNewEventRegistration = em.merge(oldEventIdOfEventRegistrationCollectionNewEventRegistration);
                    }
                }
            }
            for (EventComment eventCommentCollectionNewEventComment : eventCommentCollectionNew) {
                if (!eventCommentCollectionOld.contains(eventCommentCollectionNewEventComment)) {
                    EventLibrary oldEventIdOfEventCommentCollectionNewEventComment = eventCommentCollectionNewEventComment.getEventId();
                    eventCommentCollectionNewEventComment.setEventId(eventLibrary);
                    eventCommentCollectionNewEventComment = em.merge(eventCommentCollectionNewEventComment);
                    if (oldEventIdOfEventCommentCollectionNewEventComment != null && !oldEventIdOfEventCommentCollectionNewEventComment.equals(eventLibrary)) {
                        oldEventIdOfEventCommentCollectionNewEventComment.getEventCommentCollection().remove(eventCommentCollectionNewEventComment);
                        oldEventIdOfEventCommentCollectionNewEventComment = em.merge(oldEventIdOfEventCommentCollectionNewEventComment);
                    }
                }
            }
            for (EventGift eventGiftCollectionNewEventGift : eventGiftCollectionNew) {
                if (!eventGiftCollectionOld.contains(eventGiftCollectionNewEventGift)) {
                    EventLibrary oldEventIdOfEventGiftCollectionNewEventGift = eventGiftCollectionNewEventGift.getEventId();
                    eventGiftCollectionNewEventGift.setEventId(eventLibrary);
                    eventGiftCollectionNewEventGift = em.merge(eventGiftCollectionNewEventGift);
                    if (oldEventIdOfEventGiftCollectionNewEventGift != null && !oldEventIdOfEventGiftCollectionNewEventGift.equals(eventLibrary)) {
                        oldEventIdOfEventGiftCollectionNewEventGift.getEventGiftCollection().remove(eventGiftCollectionNewEventGift);
                        oldEventIdOfEventGiftCollectionNewEventGift = em.merge(oldEventIdOfEventGiftCollectionNewEventGift);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = eventLibrary.getEventId();
                if (findEventLibrary(id) == null) {
                    throw new NonexistentEntityException("The eventLibrary with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            EventLibrary eventLibrary;
            try {
                eventLibrary = em.getReference(EventLibrary.class, id);
                eventLibrary.getEventId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The eventLibrary with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<EventRegistration> eventRegistrationCollectionOrphanCheck = eventLibrary.getEventRegistrationCollection();
            for (EventRegistration eventRegistrationCollectionOrphanCheckEventRegistration : eventRegistrationCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This EventLibrary (" + eventLibrary + ") cannot be destroyed since the EventRegistration " + eventRegistrationCollectionOrphanCheckEventRegistration + " in its eventRegistrationCollection field has a non-nullable eventId field.");
            }
            Collection<EventComment> eventCommentCollectionOrphanCheck = eventLibrary.getEventCommentCollection();
            for (EventComment eventCommentCollectionOrphanCheckEventComment : eventCommentCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This EventLibrary (" + eventLibrary + ") cannot be destroyed since the EventComment " + eventCommentCollectionOrphanCheckEventComment + " in its eventCommentCollection field has a non-nullable eventId field.");
            }
            Collection<EventGift> eventGiftCollectionOrphanCheck = eventLibrary.getEventGiftCollection();
            for (EventGift eventGiftCollectionOrphanCheckEventGift : eventGiftCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This EventLibrary (" + eventLibrary + ") cannot be destroyed since the EventGift " + eventGiftCollectionOrphanCheckEventGift + " in its eventGiftCollection field has a non-nullable eventId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(eventLibrary);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<EventLibrary> findEventLibraryEntities() {
        return findEventLibraryEntities(true, -1, -1);
    }

    public List<EventLibrary> findEventLibraryEntities(int maxResults, int firstResult) {
        return findEventLibraryEntities(false, maxResults, firstResult);
    }

    private List<EventLibrary> findEventLibraryEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(EventLibrary.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public EventLibrary findEventLibrary(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(EventLibrary.class, id);
        } finally {
            em.close();
        }
    }

    public int getEventLibraryCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<EventLibrary> rt = cq.from(EventLibrary.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
